<?php

namespace SinoPHP\BbdBoss\Controller;

use Guzzle\Service\Resource\Model;
use SinoPHP\BbdBoss\AdminController;
use SinoPHP\BbdBoss\Defines\StaticConfig;
use SinoPHP\BbdBoss\Library\Dnspod;

/*
 * dnspod tool
 */

class DnspodTool extends AdminController {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 面板
     * 
     * @return
     */
    public function dashBoard() {
        $arr1 = [
            'key1',
            'key2'
        ];
        $arr2 = [
            'val1',
            'val2'
        ];
        // 创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值
        $arr = array_combine($arr2, $arr2);

        // 所有键名key转换大小写
        $arr3 = array_change_key_case($arr, CASE_UPPER);

        // 统计数组中所有的值,新数组中原数组值作为键，统计数作为值
        $arr4 = array_count_values($arr3);

        // 带索引检查计算数组的差集
        $arr5 = array_diff_assoc($arr2, $arr3);
        // 不带索引检查计算数组的差集
        $arr55 = array_diff($arr2, $arr3);


        $this->view([], 1, 'DnspodTool/dashBoard');
    }

    /**
     * 获取表单数据
     * 
     * @return
     */
    private function getValidataFormData() {
        $formData = request()->post();

        if (empty($formData['environment'])) {
            die('Invalid [环境] param!');
        }
        if (empty($formData['domain_type'])) {
            die('Invalid [域名] param!');
        }
        if (empty($formData['project'])) {
            die('Invalid [项目] param!');
        }

        // 分环境
        $environment = $formData['environment'];
        // 删除时候必须过滤上产环境
        if (stripos(request()->getAction(), "delete") && 'production' == $environment) {
            die('Production must be forbided!');
        }
        
        // 分域名[inner, outer]
        $domain_type = $formData['domain_type'];
        // 项目[fuse, self] 融合vs自建
        $project = $formData['project'];
        if ('fuse' == $project) {
            $domain_type = [
                'project' => $project,
                'label' => $domain_type
            ];
        }

        $dnspodObj = new Dnspod($domain_type, $environment);

        return $formData + ['dnspodObj' => $dnspodObj];
    }

    /**
     * 展示结果
     * 
     * @param  array  $result
     * @param  string $html
     * @return 
     */
    private function showResult($result, $html = null) {
        if (!empty($html)) {
            $this->view($result, 1, 'DnspodTool/' . $html);
            die;
        }

        echo '<pre>';
        print_r($result);
        echo '</pre>';
        die; 
    }

    /**
     * 购买域名列表
     * 
     * @return
     */
    public function domainList() {
        $dnspodObj = new Dnspod();
        $response = $dnspodObj->getDomainList();

        $this->showResult($response);
    }

    /**
     * 记录列表
     * 
     * @return
     */
    public function getRecordList() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/recordList');
            die;
        }

        $formData = $this->getValidataFormData();
        $dnspodObj = $formData['dnspodObj'];
        unset($formData['dnspodObj']);

        $dnspodData = [];
        if (!empty($formData['sub_domain'])) {
            $dnspodData['sub_domain'] = $formData['sub_domain'];
        }
        if (!empty($formData['length'])) {
            $dnspodData['length'] = $formData['length'];
        }
        if (!empty($formData['keyword'])) {
            $dnspodData['keyword'] = $formData['keyword'];
        }
        if (!empty($formData['offset'])) {
            $dnspodData['offset'] = $formData['offset'];
        }

        $response = $dnspodObj->getRecordList($dnspodData);

        $this->assign("search", $formData);
        $this->showResult($response, 'recordList');
    }

    /**
     * ajax 删除记录
     * 
     * @return
     */
    public function deleteRecordAjax() {
        $formData = $this->getValidataFormData();
        $dnspodObj = $formData['dnspodObj'];
        unset($formData['dnspodObj']);

        $dnspodId = $formData['dnspodId'];

        $response = $dnspodObj->deleteRecord(['recordId' => $dnspodId]);

        die(json_encode($response));
    }

    /**
     * 获取日志
     * 
     * @return
     */
    public function getDnspodLog() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getDnspodLog');
            die;
        }

        $formData = $this->getValidataFormData();
        $logNum = $formData['log_num'] ? : 20;
        $dnspodObj = $formData['dnspodObj'];
        $logList = $dnspodObj->getRecordLogList(['length' => $logNum]);

        $this->showResult($logList['log']);
    }

    /**
     * 获取记录信息
     * 
     * @return
     */
    public function getRecordInfo() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getRecordInfo');
            die;
        }

        $formData = $this->getValidataFormData();
        $dnspodObj = $formData['dnspodObj'];
        $dnspodId = $formData['dnspod_id'];
        
        $recordInfo = $dnspodObj->getRecord(['recordId' => $dnspodId]);

        $this->showResult($recordInfo);
    }

    /**
     * 删除记录
     * 
     * @return
     */
    public function deleteRecord() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/deleteRecord');
            die;
        }

        $formData = $this->getValidataFormData();
        $dnspodObj = $formData['dnspodObj'];
        $recordId = empty($formData['recordId']) ? 0 : $formData['recordId'];

        $response = $dnspodObj->deleteRecord(['recordId' => $recordId]);

        $this->showResult($response);
    }

    /**
     * 获取监控信息
     * 
     * @return
     */
    public function getMonitorInfo() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getMonitorInfo');
            die;
        }

        $formData = $this->getValidataFormData();
        $dnspodObj = $formData['dnspodObj'];
        $monitorId = $formData['monitor_id'];

        $monitorInfo = $dnspodObj->getMonitor(['monitorId' => $monitorId]);

        $this->showResult($monitorInfo);
    }

    public function logout() {
        $loginToken = getAdminUserLoginToken();

        // 记录退出登录日志
        $userLoginInfo = session($loginToken); // 从SESSION获取用户登录信息
        // $userLoginInfo && $this->addActionLog($userLoginInfo['email'] . '退出登录');
        session($loginToken, null); // 删除登录相关信息
        redirect('/AdminUser/login');

        // if (request()->isPost()) {
        //     $this->success([], '退出成功');
        // } else {
        //     redirect('/');
        // }
    }

}
